<template>
  <div class="main">
    <div class="learning">
      <el-tabs tab-position="left" style="min-height: 520px;" :stretch="true">
        <el-tab-pane label="学习中">
          <el-empty v-if="learnings.filter(learning => learning.progress < learning.route.nodes.length).length === 0"
            description="好像什么也没学呢"></el-empty>
          <div v-else class="learning-list">
            <div class="learning-list-item"
              v-for="learning in learnings.filter(learning => learning.progress < learning.route.nodes.length).slice((routePage - 1) * 5, routePage * 5)"
              :key="learning.route.id" @click="goRoute(learning.route.id)">
              <div class="learning-list-item-info">
                <div class="learning-list-item-cover">
                  <img :src="learning.route.cover" alt="">
                </div>
                <div class="learning-list-item-text">
                  <h1 class="learning-list-item-title">
                    {{ learning.route.title }}
                  </h1>
                  <div class="description">
                    <span class="learning-list-item-description">
                      {{ learning.route.description }}
                    </span>
                  </div>
                  <div>
                    <span class="learning-list-item-author" @click="goUser(learning.route.author)">
                      <i class="el-icon-user-solid"></i>
                      {{ learning.authorName }}
                    </span>
                  </div>
                </div>
              </div>
              <div>
                <div class="learning-list-item-progress">学习进度</div>
                <el-progress type="line"
                  :percentage="Math.floor((learning.progress / learning.route.nodes.length) * 100)">
                </el-progress>
              </div>
            </div>
            <el-pagination :hide-on-single-page="false" :total="learnings.length" :page-size="5"
              layout="prev, pager, next" @current-change="changePage">
            </el-pagination>
          </div>
        </el-tab-pane>
        <el-tab-pane label="已完成">
          <el-empty v-if="learnings.slice(learning => learning.progress == learning.route.nodes.length).length === 0"
            description="好像什么也没学呢"></el-empty>
          <div v-else class="learning-list">
            <div class="learning-list-item"
              v-for="learning in learnings.filter(learning => learning.progress == learning.route.nodes.length).slice((finishPage - 1) * 5, finishPage * 5)"
              :key="learning.route.id" @click="goRoute(learning.route.id)">
              <div class="learning-list-item-info">
                <div class="learning-list-item-cover">
                  <img :src="learning.route.cover" alt="">
                </div>
                <div class="learning-list-item-text">
                  <h1 class="learning-list-item-title">
                    {{ learning.route.title }}
                  </h1>
                  <div class="description">
                    <span class="learning-list-item-description">
                      {{ learning.route.description }}
                    </span>
                  </div>
                  <div>
                    <span class="learning-list-item-author" @click="goUser(learning.route.author)">
                      <i class="el-icon-user-solid"></i>
                      {{ learning.authorName }}
                    </span>
                  </div>
                </div>
              </div>
              <div>
                <div class="learning-list-item-progress">学习进度</div>
                <el-progress type="line"
                  :percentage="Math.floor((learning.progress / learning.route.nodes.length) * 100)">
                </el-progress>
              </div>
            </div>
            <el-pagination :hide-on-single-page="false" :total="learnings.length" :page-size="5"
              layout="prev, pager, next" @current-change="changeFinishPage">
            </el-pagination>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import request from '@/API/request';
import { Message } from 'element-ui';
export default {
  name: 'LearningInfo',
  mounted() {
    request.get('/route/user/learn', {
      params: {
        id: this.$route.query.id,
      },
    }).then((res) => {
      if (res.data.code === 200) {
        this.learnings = res.data.data;
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch((err) => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    this.loading = false;
  },
  data() {
    return {
      routePage: 1,
      finishPage: 1,
      learnings: [],
      loading: true,
    }
  },
  methods: {
    goRoute(id) {
      this.$router.push({
        path: '/route',
        query: {
          id,
        },
      });
    },
    goUser(id) {
      this.$router.push({
        path: '/user',
        query: {
          id,
        },
      });
    },
    changePage(page) {
      this.routePage = page
    },
    changeFinishPage(page) {
      this.finishPage = page
    },
  }
}
</script>

<style scoped>
.main {
  width: 100%;
  min-height: 520px;
  background-color: #fff;
}

.learning {
  text-align: left;
  margin: 5px;
}

.title {
  display: block;
  font-size: 18px;
  line-height: 20px;
  font-weight: 500;
  margin: 10px;
}

.el-divider {
  margin-top: 10px;
  margin-bottom: 10px;
}

.learning-list-item {
  border: solid 1px rgba(207, 205, 205, 0.842);
  padding: 3px;
  margin-top: 10px;
  margin-bottom: 10px;
  border-radius: 4px;
  transition: all 0.5s ease;
}

.learning-list-item:hover {
  border: solid 1px #409eff;
  box-shadow: 0 0 10px #409eff;
  cursor: pointer;
}

.learning-list-item-cover {
  display: inline-block;
  border-radius: 4px;
  position: relative;
  top: -15px;
  border-radius: 4px;
}

img {
  width: 200px;
  height: 140px;
  border-radius: 4px;
  object-fit: cover;
}

.learning-list-item-text {
  display: inline-block;
  margin-left: 10px;
  position: relative;
  text-align: left;
  height: 140px;
  top: 15px;
  width: 70%;
}

.learning-list-item-title {
  position: relative;
  top: 0px;
  display: inline-block;
  line-height: 35px;
  margin-left: 10px;
  margin-bottom: 0px;
  width: 80%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.learning-list-item-author {
  position: relative;
  display: inline-block;
  font-size: 13px;
  margin-left: 10px;
  margin-top: 10px;
  top: 30px;
  border-radius: 1px;
  transition: all 0.3s ease;
}

.learning-list-item-author:hover {
  box-shadow: #235588 0px 0px 0px 1px;
  color: #235588;
  cursor: pointer;
}

.learning-list-item-description {
  display: inline-block;
  font-size: 13px;
  margin-left: 10px;
  margin-top: 10px;
  color: rgb(134, 134, 134);
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.description {
  width: 80%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.learning-list-item-progress {
  font-size: 15px;
  margin-left: 10px;
}

.el-progress {
  margin-left: 10px;
}

.el-pagination {
  text-align: center;
}
</style>